<?php


namespace app\api\controller\v1;

use think\model\Pivot;

class Comment extends Base
{
    //所有权限列表树状数据
   public function getTree($list)
   {
       $pid1 = []; //一级目录列表

       $pid2 = []; //二级目录列表

       //遍历出二级目录的个数

       foreach($list as $key => $value){
           if($value['pid'] !== 0){
               $pid2[] = $value;
           }
       }

       //将三级目录存入二级目录
       foreach ($pid2 as $key => $value){
           foreach($list as $k => $v){
               if($value['id'] === $v['pid']){
                   $pid2[$key]['children'][] = $v;
               }
           }
       }

       //遍历一级目录个数
       foreach($list as $key => $value){
          if($value['pid'] === 0){
              $pid1[] = $value;
          }
       }

       //将二级目录存入一级目录中
       foreach($pid1 as $key => $value){
           foreach($pid2 as $k => $v){
               if($value['id'] === $v['pid']){
                   $pid1[$key]['children'][] = $v;
               }
           }
       }

       if(empty($pid1)){
           return $this->out([],'获取权限列表失败',400);
       }

       return $this->out($pid1,'获取权限列表成功',200);
   }

    //左侧菜单权限列表树状数据
    public function getMenus($list)
    {
        $pid = []; //一级目录列表

        //遍历一级目录个数
        foreach($list as $key => $value){
            if($value['pid'] === 0){
                $pid[] = $value;
            }
        }

        //将二级目录存入一级目录中
        foreach($pid as $key => $value){
            foreach($list as $k => $v){
                if($value['id'] === $v['pid']){
                    unset($v['pid']);
                    $pid[$key]['children'][] = $v;
                }
            }
            unset($pid[$key]['pid']);
        }

        if(empty($pid)){
            return $this->out([],'获取菜单列表失败',400);
        }

        return $this->out($pid,'获取菜单列表成功',200);
    }

    //角色列表树状数据
    public function getRoles($list,$roles){

       $ra = [];  //角色权限列表

       $raMessage = [];  //角色的权限列表信息

       foreach($roles as $key => $value){
           $ra[] = explode(',',$value['ps_ids']);

           foreach($list as $k => $v){
               if(in_array($v['id'],$ra[$key])){
                   $raMessage[$key][] = $v;
               }
           }
       }

        $pid1 = []; //一级目录列表

        $pid2 = []; //每个角色的二级权限目录

        //遍历出每个角色的权限二级目录
        foreach($raMessage as $key => $value){
            foreach($value as $k => $v){
                if($v['pid'] !== 0){
                    $pid2[$key][] = $v;
                }
            }
        }

        //将每个角色权限的三级目录存入二级目录
        foreach($pid2 as $key => $value){
            foreach($value as $k1 => $v1){
                foreach($value as $k2 => $v2){
                    if($v1['id'] === $v2['pid']){
                        $pid2[$key][$k1]['children'][] = $v2;
                        unset($pid2[$key][$k2]);
                    }
                }
            }
        }

        //遍历每个角色权限一级目录个数
        foreach($raMessage as $key => $value){
            foreach($value as $k => $v){
                if($v['pid'] === 0){
                    $pid1[$key][] = $v;
                }
            }
        }

        //将二级目录存入一级目录中
        foreach($pid1 as $key => $value){
           foreach($value as $k => $v){
               foreach($pid2 as $k1 => $v1){
                   foreach($v1 as $k2 => $v2){
                      if($v2['pid'] === $v['id']){
                          $pid1[$key][$k]['children'][] = $v2;
                      }
                   }
               }
           }
        }

        //将一级目录存入角色目录中
        foreach($roles as $key => $value){
            foreach($pid1 as $k => $v){
                if($key === $k){
                    $roles[$key]['children'][] = $v;
                }
            }
            unset($roles[$key]['ps_ids']);
        }

        if(empty($roles)){
            return $this->out([],'获取失败',400);
        }

        return $this->out($roles,'获取成功',200);
    }

    //删除角色指定权限树状数据
    public function delAuth($list,$ra)
    {
        //获取角色新权限的权限信息

        $raMessage = [];

        foreach($ra as $key => $value){
            foreach($list as $k => $v){
                if($v['id'] == $value){
                    $raMessage[] = $v;
                }
            }
        }

        $pid1 = []; //一级权限栏目
        $pid2 = []; //二级权限栏目

        //获取新角色权限列表二级栏目
        foreach($raMessage as $key => $value){
           if($value['pid'] !== 0){
               $pid2[] = $value;
           }
        }


        //获取三级栏目存入二级栏目中

        foreach($pid2 as $key => $value){
            foreach($pid2 as $k => $v){
                if($value['id'] === $v['pid']){
                    unset($v['pid']);
                    $pid2[$key]['children'][] = $v;
                    unset($pid2[$k]);
                }
            }
        }

        //获取一级栏目
        foreach($raMessage as $key => $value){
            if($value['pid'] === 0){
                $pid1[] = $value;
            }
        }

        //将二级栏目存入一级栏目
        foreach($pid1 as $key => $value){
            foreach($pid2 as $k => $v){
                if($value['id'] === $v['pid']){
                    unset($v['pid']);
                    $pid1[$key]['children'][] = $v;
                }
            }
            unset($pid1[$key]['pid']);
        }

        if(empty($pid1)){
            return $this->out([],'取消权限失败',400);
        }

        return $this->out($pid1,'取消权限成功',200);
    }

    //商品分类树状数据
    public function getGoods($list,$type)
    {
        $pid = [];  //一级目录
        $pid2 = []; //二级目录

        //获取一级目录
        foreach($list as $key => $value){
            if($value['cat_pid'] === 0){
                $pid[] = $value;
            }
        }

        switch($type)
        {
            case 1:
                if(empty($pid)){
                    return $this->out([],'获取失败',400);
                }
                return $this->out($pid,'获取成功',200);
                break;
            case 2:
                //将二级目录存入一级目录的中
                foreach($pid as $key => $value){
                    foreach($list as $k => $v){
                        if($value['cat_id'] === $v['cat_pid']){
                            $pid[$key]['children'][] = $v;
                        }
                    }
                }

                if(empty($pid)){
                    return $this->out([],'获取失败',400);
                }
                return $this->out($pid,'获取成功',200);
                break;
            case 3:
                //获取二级目录
                foreach($list as $key => $value){
                    if($value['cat_pid'] !== 0){
                        $pid2[] = $value;
                    }
                }

                foreach($pid2 as $key => $value){
                    foreach($pid2 as $k => $v){
                        if($value['cat_id'] === $v['cat_pid']){
                            $pid2[$key]['children'][] = $v;
                            unset($pid2[$k]);
                        }
                    }
                }

                //把二级目录存入一级目录中
                foreach($pid as $key => $value){
                    foreach($pid2 as $k => $v){
                        if($value['cat_id'] === $v['cat_pid']){
                            $pid[$key]['children'][] = $v;
                        }
                    }
                }

                if(empty($pid)){
                    return $this->out([],'获取失败',400);
                }
                return $this->out($pid,'获取成功',200);
                break;
            default:
                //获取二级目录
                foreach($list as $key => $value){
                    if($value['cat_pid'] !== 0){
                        $pid2[] = $value;
                    }
                }

                foreach($pid2 as $key => $value){
                    foreach($pid2 as $k => $v){
                        if($value['cat_id'] === $v['cat_pid']){
                            $pid2[$key]['children'][] = $v;
                            unset($pid2[$k]);
                        }
                    }
                }

                //把二级目录存入一级目录中
                foreach($pid as $key => $value){
                    foreach($pid2 as $k => $v){
                        if($value['cat_id'] === $v['cat_pid']){
                            $pid[$key]['children'][] = $v;
                        }
                    }
                }
                if(empty($pid)){
                    return $this->out([],'获取失败',400);
                }
                return $this->out($pid,'获取成功',200);
        }
    }
}